const BaseRest = require('../rest.js');
module.exports = class extends BaseRest
{

    constructor(ctx)
    {
        super(ctx);
        this._judgerModel = this.model('apiv1/judger');
        this._judgerService = this.service('apiv1/judger');
    }

    async postAction()
    {
        let postData = this.post('data');
        if (think.isEmpty(postData))
        {
            return this.error(52001, "提交的表单信息错误,请检查数据格式,上传数据格式为{'data',yourJsonData},如 : $.post(\"http://127.0.0.1:8091/apiv1/competition\",{'data':yourJsonData}),yourJsonData为你要上传的数据包");
        }
        let postDataObj;
        try
        {
            postDataObj = JSON.parse(postData);
        } catch (e)
        {
            //记录日志
            return this.error(52001, "提交的表单信息错误,请检查数据格式,上传数据格式为{'data',yourJsonData},如 : $.post(\"http://127.0.0.1:8091/apiv1/competition\",{'data':yourJsonData}),yourJsonData为你要上传的数据包");
        }

        let service=this._judgerService.getJudgerData(postDataObj);
        if (service.isErr === false)
        {
            let dbRes = await this._judgerService.addData();
            return this.error(dbRes.errNo, dbRes.errMsg);
        }
        return this.error(service.errNo, service.errMsg);
    }


    async getAction()
    {
        let data;
        if (this._guid)
        {
            let data = await this._judgerModel.where({'PK_JudgerGuid': this._guid}).find();
            return this.end(data);
        }

        data = await this._judgerModel.getAllJudger();
        return this.end(data);
    }


    /**
     * update resource
     * @return {Promise} []
     */
    async putAction()
    {
        if (!this._guid)
        {
            return this.error(52017,"guid不存在,请检查guid");
        }
        let postData = this.post('data');
        if (think.isEmpty(postData))
        {
            return this.error(52001, "提交的表单信息错误,请检查数据格式,上传数据格式为{'data',yourJsonData},如 : $.post(\"http://127.0.0.1:8091/apiv1/competition\",{'data':yourJsonData}),yourJsonData为你要上传的数据包");
        }
        let postDataObj;
        try
        {
            postDataObj = JSON.parse(postData);
        } catch (e)
        {
            //记录日志
            return this.error(52001, "提交的表单信息错误,请检查数据格式,上传数据格式为{'data',yourJsonData},如 : $.post(\"http://127.0.0.1:8091/apiv1/competition\",{'data':yourJsonData}),yourJsonData为你要上传的数据包");
        }

        let service=this._judgerService.getJudgerData(postDataObj);
        if (service.isErr === false)
        {
            let dbRes = await this._judgerService.updateData(this._guid);
            return this.error(dbRes.errNo, dbRes.errMsg);
        }
        return this.error(service.errNo, service.errMsg);
    }


    /**
     * delete resource
     * @return {Promise} []
     */
    async deleteAction()
    {
        if (!this._guid) {
            return this.error(52019,'删除评委时的guid为空,请检查guid')
        }
        let res=await this._judgerModel.where({'PK_JudgerGuid':this._guid}).update({'IsDelete':"已删除"});

        if(res===1){
            return this.error(0,"删除成功");
        }else {
            return this.error(52020,"主键为该guid的评委不存在或该条记录已修改,请检查guid");
        }
    }
};